<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper ContractPurchasePaymentManage">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter='24'>
          <a-col :xl="24" :lg="7" :md="8" :sm="24">
            <a-radio-group v-model="queryParam.paymentStatus" @change="radioChange">
              <a-form-item label="付款状态">
                <a-radio :value="4">本年未付</a-radio>
                <a-radio :value="5">本年已付</a-radio>
                <a-radio :value="0">未付</a-radio>
                <a-radio :value="1">已付</a-radio>
                <a-radio :value="2">已审未付</a-radio>
                <a-radio :value="3">全部</a-radio>
              </a-form-item>
            </a-radio-group>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户简称">
              <a-input placeholder="请输入客户简称" v-model="queryParam.customerName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="合同编号">
              <a-input placeholder="请输入合同编号" v-model="queryParam.purchaseNum"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="合同名称">
                <a-input placeholder="请输入合同名称" v-model="queryParam.purchaseName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="项目经理">
                <a-input placeholder="请输入项目经理" v-model="queryParam.projectCharger"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="审核状态">
                <j-dict-select-tag
                  type="list"
                  v-model="queryParam.bpmStatus"
                  dictCode="bpm_status"
                  placeholder="请选择审核状态"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="8" :sm="12">
              <a-form-item label="计划付款时间">
                <j-date
                  placeholder="请选择开始日期"
                  class="query-group-cust"
                  v-model="queryParam.expectTime_begin"
                ></j-date>
                <span class="query-group-split-cust"></span>
                <j-date
                  placeholder="请选择结束日期"
                  class="query-group-cust"
                  v-model="queryParam.expectTime_end"
                ></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="8" :sm="12">
              <a-form-item label="实际付款时间">
                <j-date
                  placeholder="请选择开始日期"
                  class="query-group-cust"
                  v-model="queryParam.actualTime_begin"
                ></j-date>
                <span class="query-group-split-cust"></span>
                <j-date
                  placeholder="请选择结束日期"
                  class="query-group-cust"
                  v-model="queryParam.actualTime_end"
                ></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('采购合同-付款申请')"
        v-has="'ContractPurchasePaymentManage:export'"
        >导出</a-button>
      <!-- 高级查询区域 -->
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="customerName" slot-scope="text, record">
          <a @click="paymentApplyHandleDetail(record, true)">{{ text }}</a>
        </template>
        <template slot="bpmStatus_dictText" slot-scope="text, record">
          <BpmStatusTagComponent :record="record" :text="text"></BpmStatusTagComponent>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="payCondition" slot-scope="text">
          <j-ellipsis :value="text" :length="15" />
        </template>
        <template slot="purchaseName" slot-scope="text">
          <j-ellipsis :value="text" :length="10" />
        </template>
        <template slot="supplierName" slot-scope="text">
          <j-ellipsis :value="text" :length="10" />
        </template>
        <span slot="action" slot-scope="text, record">
          <div>
            <a
              v-has="'ContractPurchasePaymentManage:edit'"
              @click="handleEdit(record, false,'编辑','edit')"
              v-if="record.bpmStatus === '1'"
              >编辑</a
            >
            <a-divider type="vertical" v-if="record.bpmStatus === '1'" v-has="'ContractPurchasePaymentManage:edit'" />
            <a
              v-if="record.bpmStatus === '1'"
              v-has="'ContractPurchaseApplyPayment:edit'"
              @click="paymentApplyHandleDetail(record)"
              >申请</a
            >
            <a-divider type="vertical" v-has="'ContractPurchaseApplyPayment:edit'" v-if="record.bpmStatus === '1' && record.auditDataId" />
            <template v-if="record.auditDataId">
              <a-dropdown v-has="'ContractPurchaseApplyPayment:edit'">
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item v-if="record.bpmStatus === '1'">
                  <a @click="startProcess(record)">发起流程</a>
                </a-menu-item>
                <a-menu-item v-if="record.bpmStatus !== '1'" @click="handleTrack(record)">审批进度</a-menu-item>
              </a-menu>
            </a-dropdown>
            </template>

          </div>
          <div v-has="'ContractPurchase:sure'">
            <span v-has="'ContractPurchase:sure'">
              <a v-if="record.auditDataId" @click="paymentApplyHandleDetail(record, true)">详情</a>
              <a-divider type="vertical" v-if="record.auditDataId" />
              <a @click="handleEdit(record, false,'确认','sure')">确认</a>
            </span>
          </div>
        </span>
      </a-table>
      <a-row>
        <a-col :span="12">
          <a-card>
            <a-statistic title="计划付款金额（元）" :value="countExpectMoney" />
          </a-card>
        </a-col>
        <a-col :span="12">
          <a-card>
            <a-statistic title="实际付款金额（元）" :value="countActualMoney" />
          </a-card>
        </a-col>
      </a-row>
    </div>

    <contract-purchase-apply-payment-modal
      ref="paymentApplyModalForm"
      @ok="modalFormOk"
    ></contract-purchase-apply-payment-modal>
    <contract-purchase-payment-modal ref="modalForm" @ok="modalFormOk"></contract-purchase-payment-modal>
    <bpm-process-track-modal ref="bpmProcessTrackModal"></bpm-process-track-modal>
    <bpm-biz-task-deal-modal
      ref="taskDealModal"
      :path="path"
      :formData="formData"
      @ok="handleOk"
    ></bpm-biz-task-deal-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgBpmListMixin } from '@/mixins/JeecgBpmListMixin'
import ContractPurchaseApplyPaymentModal from './modules/ContractPurchaseApplyPaymentModal'
import ContractPurchasePaymentModal from './modules/ContractPurchasePaymentModal'
import { postAction, getAction } from '@/api/manage'
import BpmBizTaskDealModal from '@views/modules/bpmbiz/common/BpmBizTaskDealModal'
import BpmProcessTrackModal from '@views/modules/bpmbiz/common/BpmProcessTrackModal'
import { toFixedTwo } from '@/utils/util'
import { loginUserButtonAuth } from '@/utils/loginUserButtonAuth'
import BpmStatusTagComponent from '@/components/bpm/bpmStatusTagComponent'

export default {
  name: 'ContractPurchaseApplyPaymentList',
  mixins: [JeecgBpmListMixin, mixinDevice],
  components: {
    ContractPurchaseApplyPaymentModal,
    BpmBizTaskDealModal,
    BpmProcessTrackModal,
    ContractPurchasePaymentModal,
    BpmStatusTagComponent,
  },
  data() {
    return {
      description: '采购合同-付款申请管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '采购合同编号',
          align: 'center',
          dataIndex: 'purchaseNum',
          scopedSlots: { customRender: 'customerName' },
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerName',

        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName',
        },
        {
          title: '项目经理',
          align: 'center',
          dataIndex: 'projectCharger',
        },
        {
          title: '经办人',
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: '采购合同名称',
          align: 'center',
          dataIndex: 'purchaseName',
          scopedSlots: { customRender: 'purchaseName' },
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'supplierName',
          scopedSlots: { customRender: 'supplierName' },
        },
        {
          title: '付款条件',
          align: 'center',
          dataIndex: 'payCondition',
          scopedSlots: { customRender: 'payCondition' },
        },
        {
          title: '计划付款金额',
          align: 'right',
          dataIndex: 'expectMoney',
          sorter: true,
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          },
        },
        {
          title: '计划付款时间',
          align: 'center',
          dataIndex: 'expectTime',
          sorter: true,
        },
        {
          title: '实际付款金额',
          align: 'right',
          dataIndex: 'actualMoney',
          sorter: true,
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          },
        },
        {
          title: '实际付款时间',
          align: 'center',
          dataIndex: 'actualTime',
          sorter: true,
        },
        {
          title: '是否收票',
          align: 'center',
          dataIndex: 'billing_dictText',
        },
        {
          title: '付款完成',
          align: 'center',
          dataIndex: 'paymentCompleted_dictText',
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'bpmStatus_dictText',
          scopedSlots: { customRender: 'bpmStatus_dictText' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/contract/contractPurchasePayment/list',
        sum: '/contract/contractPurchasePayment/sum',
        delete: '/contract/contractPurchaseApplyPayment/delete',
        deleteBatch: '/contract/contractPurchaseApplyPayment/deleteBatch',
        exportXlsUrl: '/contract/contractPurchasePayment/exportXls',
        importExcelUrl: 'contract/contractPurchaseApplyPayment/importExcel',
        startProcess: '/act/process/extActProcess/startMutilProcess',
        findByPaymentId: '/contract/contractPurchaseApplyPayment/findByPaymentId',
        queryById: '/contract/contractPurchasePayment/queryById',
      },
      countExpectMoney: '',
      countActualMoney: '',
      //代码生成后需手动设置流程编码
      flowCode: 'dev_hf_contract_purchase_apply_payment_001',
      dictOptions: {},
      superFieldList: [],
      queryModel: true,
      queryParam: {
        paymentStatus: 4, //默认付款状态为未回
        contractBpmStatus: '3', //查询审核通过后的合同
      },
    }
  },
  created() {
    loginUserButtonAuth(['ContractPurchasePaymentManage:edit','ContractPurchaseApplyPayment:edit','ContractPurchase:sure'], this.columns)
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.getSum(params)
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.dataSource.forEach((v) => {
            v.isPay = v.actualTime ? '是' : '否'
          })
          this.ipagination.total = res.result.total
        }
        this.loading = false
      })
    },
    startProcess(record) {
      this.$confirm({
        title: '提示',
        content: '确认提交流程吗?',
        onOk: () => {
          let params = {
            flowCode: this.flowCode,
            id: record.auditDataId,
            formUrl: 'hifar/contract/modules/ContractPurchaseApplyPaymentForm',
            formUrlMobile: '',
          }
          postAction(this.url.startProcess, params)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.loadData()
                this.onClearSelected()
              } else {
                this.$message.warning(res.message)
              }
            })
            .catch((e) => {
              this.$message.warning('不识别的请求!')
            })
        },
      })
    },
    handleEdit(record, disableSubmit,action,type) {
      getAction(this.url.queryById, { id: record.id }).then((res) => {
        if (res.success) {
          res.result.type = type
          this.$refs.modalForm.edit(res.result)
          this.$refs.modalForm.title = action
          this.$refs.modalForm.disableSubmit = disableSubmit
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleTrack(record) {
      var params = { flowCode: this.flowCode, dataId: record.auditDataId } //查询条件
      this.$refs.bpmProcessTrackModal.handleTrack(params)
      this.$refs.bpmProcessTrackModal.title = '审批跟踪记录'
    },
    paymentApplyHandleDetail(record, disableSubmit) {
      getAction(this.url.findByPaymentId, { id: record.id }).then((res) => {
        if (res.success) {
          this.$refs.paymentApplyModalForm.edit(res.result)
          this.$refs.paymentApplyModalForm.title = '编辑'
          this.$refs.paymentApplyModalForm.disableSubmit = disableSubmit
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    getSum(params) {
      getAction(this.url.sum, params).then((res) => {
        if (res.success) {
          this.countExpectMoney = res.result.countExpectMoney.toFixed(2)
          this.countActualMoney = res.result.countActualMoney.toFixed(2)
        }
      })
    },
    searchReset() {
      this.queryParam = {}
      this.queryParam.paymentStatus = 4
      this.queryParam.contractBpmStatus = '3'
      this.loadData(1)
    },
    radioChange() {
      this.loadData()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
<style>
.ContractPurchasePaymentManage .ant-radio-group {
  display: block;
}
</style>